package com.jtjrenren.android.taxi.passenger.base;

import android.app.Fragment;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;


import com.library.weidget.CustomeWaittingDialog;
import com.library.utils.StringUtils;

/**
 * Created by Administrator on 2015/12/21.
 */
public abstract class BaseFragment extends Fragment {
    public static final int CODE_SHOW = 300;
    public static final int CODE_BDMAP_LOC = 301; //百度地图定位成功

    /**
     * 用于和activity之间通信
     */
    public interface ConnToConListener {
        void connToCon(Message msg);
    }

    protected ConnToConListener connListener; //用于和activity之间通信


    private String titleDesc; //用于展示在title上
    protected Handler handler;


    protected void initHandler() {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case WHAT_AUTHCODE_REFRESH:
                        refreshAuthCode();
                        break;
                    case CODE_SHOW:
                        displayShort(msg.obj.toString());
                        break;
                    default:
                        handMsg(msg);
                        break;
                }
            }
        };
    }

    protected void handMsg(Message msg) {
    }

    protected void displayShort(String msg) {
        Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
    }


    /**
     * 将消息发送到activity容器
     *
     * @param msg
     */
    protected void sendMsgToActivity(Message msg) {
        if (connListener != null)
            connListener.connToCon(msg);
    }

    protected void sendMsgToActivity(int what) {
        sendMsgToActivity(what, null);
    }

    protected void sendMsgToActivity(int what, Object obj) {
        Message msg = Message.obtain();
        msg.what = what;
        if (obj != null)
            msg.obj = obj;
        sendMsgToActivity(msg);
    }


    public String getTitleDesc() {
        if (titleDesc == null)
            titleDesc = "";
        return titleDesc;
    }

    public void setTitleDesc(String titleDesc) {
        this.titleDesc = titleDesc;
    }

    public void setConnListener(ConnToConListener connListener) {
        this.connListener = connListener;
    }


    /*  刷新dialog */
    protected CustomeWaittingDialog waittingDialog;

    protected void showWaittingDialog(String title, boolean outCancle) {
        if (waittingDialog == null)
            waittingDialog = new CustomeWaittingDialog(getActivity(), title);
        else
            waittingDialog.setDesc(title);
        waittingDialog.setCanceledOnTouchOutside(outCancle);
        waittingDialog.show();
    }

    protected void dismissWaittingDialog() {
        if (waittingDialog != null && waittingDialog.isShowing())
            waittingDialog.dismiss();
    }


    /* 验证码有关 */
    protected static final int WHAT_AUTHCODE_REFRESH = 303;
    protected static final int WHAT_AUTHCODE_FINISH = 304;
    private int codetimeCount = 60;  //获取验证码等待秒数
    private static final int TIME_CODE_DEALY = 1 * 1000; //验证码刷新间隔 1s
    protected boolean isGetAuthCode = false; //是否正在获取验证码

    /**
     * 开启验证码记时
     */
    protected void startAuthCode() {
        if (handler == null)
            return;
        clearRefreshAuthCode();

        isGetAuthCode = true;

        handler.sendEmptyMessage(WHAT_AUTHCODE_REFRESH);
    }

    /**
     * 验证码记时更新
     */
    private void refreshAuthCode() {
        Message msg = Message.obtain();
        if (codetimeCount == 0) {
            clearRefreshAuthCode();
            msg.what = WHAT_AUTHCODE_FINISH;
        } else {
            codetimeCount--;
            msg.what = WHAT_AUTHCODE_REFRESH;
            msg.obj = codetimeCount;
            handler.sendEmptyMessageDelayed(WHAT_AUTHCODE_REFRESH, TIME_CODE_DEALY);
        }
        handMsg(msg);
    }

    /**
     * 清除获取验证码的状态
     */
    protected void clearRefreshAuthCode() {
        codetimeCount = 60;
        handler.removeMessages(WHAT_AUTHCODE_REFRESH);
        isGetAuthCode = false;
    }

    //
    public abstract int getLayoutId();

    /**
     * 在oncreateview中调用
     *
     * @param view
     */
    protected abstract void initViews(View view);

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(getLayoutId(), container, false);
        initViews(view);
        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (handler != null)
            handler.removeMessages(WHAT_AUTHCODE_REFRESH);
    }


    private ProgressDialog proDialog;

    protected void showProDialog(String title, String desc) {
        if (proDialog == null)
            proDialog = new ProgressDialog(getActivity(),ProgressDialog.THEME_HOLO_DARK);
        proDialog.setCanceledOnTouchOutside(false);

        if (!StringUtils.isEmpty(title))
            proDialog.setTitle(title);
        if (!StringUtils.isEmpty(desc))
            proDialog.setMessage(desc);

        proDialog.show();
    }

    protected void disProDialog() {
        if (proDialog != null)
            proDialog.dismiss();
    }

}
